import { existsSync, fstat, readFileSync, writeFileSync } from "fs";
import { json } from "stream/consumers";

type BaseType = "boolean" | 'short' | 'word' | 'long'
    | 'dword' | 'float' | 'double'
    | 'string' | 'bcd' | 'sbcd';
type cfgTag = {
    name: string;
    addr: string;
    datatype: BaseType;
    length?: number;
    precision?:number;
    readonly?: boolean;
};
type cfgDevice = {
    server: string,
    scanrate: number,
    addr:number,
    timeout: number,
    tags: cfgTag[],
}
type cfgServer = {
    port: number,
    devices?: {
        [key: string]: cfgDevice;
    };
}
export {
    BaseType,
    cfgServer,
    cfgDevice,
    cfgTag,
    getconfig,
    configfile,
}
const configfile = `/etc/agilor/modbustcp.json`;
function getconfig(cf: string): cfgServer {
    if (existsSync(cf)) {
        const fs = readFileSync(cf);
        return JSON.parse(fs.toString("utf8"));
    } else {
        return { port: 2317 };
    }
}
